
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @search-reset="searchReset"
        @refresh-change="refreshChange"
        :cell-style="cellStyle"
      >
        <!-- <template
          slot="searchMenu"
          slot-scope="{row,size}"
        >
          <el-button
            v-if="permissionList.export"
            @click="exportClick(row)"
          >导出</el-button>
        </template> -->
        <template slot="menuRight">
          <el-button
            type="primary"
            size="small"
            @click="createInventoryComparison"
          >生成复核对比</el-button>
          <el-button
            v-if="permissionList.export"
            @click="exportClick"
          >导出</el-button>
        </template>
      </avue-crud>
      <open-dialog
        :DialogVisible.sync="editVisableDialog"
        title="生成复核对比"
        width="40%"
      >
        <el-form label-width="60px" v-if="editVisableDialog">
          <avue-form
            v-model="formCreate"
            :option="createFareOption"
            style="margin-bottom: 40px"
          ></avue-form>
          <div class="bottom-menu-report">
            <el-button type="primary" :loading="loading" @click="editFare()">生成</el-button>
            <el-button type="primary" @click="editVisableDialog = false"
              >取消</el-button
            >
          </div>
        </el-form>
      </open-dialog>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, dataExport,createFares } from '@/api/rca/viewckdataauditjiehangcash'
import { tableOption, startDate, endDate } from '@/const/crud/rca/viewckdataauditjiehangcash'
import { handleExcelResponse, getQueryString } from "@/util/util";
import { mapGetters } from 'vuex'

export default {
  name: 'viewckdataauditjiehangcash',
  data () {
    return {
      searchForm: {
        tradeDate: [startDate, endDate]
      },
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 30 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,

      extraParam: {

      },
      formCreate: {},
      editVisableDialog: false,
      loading: false,
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList () {
      return {
        export: this.vaildData(this.permissions.view_ck_dataaudit_jiehangcash_export, false),
      };
    },
    createFareOption(){
      return {
        emptyBtn: false,
        submitBtn: false,
        column: [ {
          "type": "date",
          "label": "开始日期",
          "prop": "startDate",
          format: 'yyyy-MM-dd',
          valueFormat: 'yyyy-MM-dd',
          rules: [
            {
              required: true,
              message: '请选择开始日期',
              trigger: 'blur'
            }
          ]
        }, {
          "type": "date",
          "label": "结束日期",
          "prop": "endDate",
          format: 'yyyy-MM-dd',
          valueFormat: 'yyyy-MM-dd',
          rules: [
            {
              required: true,
              message: '请选择结束日期',
              trigger: 'blur'
            }
          ]
        }]
      }
    }
  },
  methods: {
    cellStyle({ row, column, rowIndex,columnIndex }) {
      if(3==columnIndex){
        if (row.diffMoney < 0) {
          return {
            color: "red",
          };
        }else if (row.diffMoney > 0) {
          return {
            color: "green",
          };
        }
      }
    },
    async getList (page, params) {
      this.tableLoading = true;
      try {
        const { tradeDate } = this.searchForm;
        const [startDate, endDate] = tradeDate;
        const response = await fetchList(
          Object.assign(
            {
              current: page.currentPage,
              size: page.pageSize,
              startDate,
              endDate,
            },
          )
        );
        this.tableData = response.data.data.records;
        this.page.total = response.data.data.total;
        this.tableLoading = false;
      } catch (e) {
        this.tableLoading = false;
      };
    },
    async exportClick (row) {
      // await this.searchChange(row, () => { });
      this.dataExport();
    },
    async dataExport () {
      const { tradeDate = [] } = this.searchForm;
      const [startDate, endDate] = tradeDate;
      let loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      })
      try {
        const res = await dataExport(Object.assign({}, this.searchForm, { startDate, endDate }));
        // handleExcelResponse(res, `${startDate}至${endDate}导出.xlsx`);
        handleExcelResponse(res);
      } finally {
        loading.close();
      }
    },
    async searchChange (form, done) {
      this.searchForm = { ...form, ...this.extraParam };
      try {
        await this.getList(this.page, this.searchForm);
      } finally {
        done();
      }
    },
    refreshChange () {
      this.getList(this.page)
    },
    searchReset () {

      this.page.currentPage = 1
      this.getList(this.page);
    },
    createInventoryComparison(){
      this.editVisableDialog = true;
    },
    async editFare(){
      this.loading = true;
      await createFares(this.formCreate)
        .then(data => {
          this.$message.success('生成成功！')
          this.editVisableDialog = false
          this.refreshChange()
          this.loading = false;
        }).catch(error=> {
          if(error.message.indexOf('timeout')!=-1){
            this.$message.warning('生成需要一定时间，请稍后查看!')
            this.editVisableDialog=false
            this.refreshChange()
            this.loading = false;
          }else{
            this.$message.error('操作失败!')
            this.refreshChange()
            this.loading = false;
          }
        });
    }
  }
}
</script>
<style lang="scss">
.bottom-menu-report {
  margin-top: -30px;
  text-align: center;
}
</style>